package com.exercise.widgets.actionbar

import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuItem
import android.view.MenuItem.OnActionExpandListener
import android.widget.Toast
import androidx.appcompat.widget.SearchView
import androidx.appcompat.widget.SearchView.OnQueryTextListener
import androidx.appcompat.widget.ShareActionProvider
import androidx.core.view.MenuItemCompat
import com.exercise.struct.base.BaseActivity
import com.exercise.struct.viewmodel.BaseViewModel
import com.exercise.widgets.R
import com.exercise.widgets.databinding.ActivityActionBarBinding

class ActionBarActivity : BaseActivity<BaseViewModel, ActivityActionBarBinding>() {
    override val bindingInflater: (LayoutInflater) -> ActivityActionBarBinding
        get() = ActivityActionBarBinding::inflate
    override val modelClass: Class<BaseViewModel>
        get() = BaseViewModel::class.java

    override fun initData() {
    }

    override fun initView() {
        setSupportActionBar(binding.toolbar)
        binding.toolbar.setNavigationOnClickListener {
            finish()
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.action_bar_menu, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            R.id.action_favorite -> {
                Toast.makeText(this, "Favorite", Toast.LENGTH_SHORT).show()
                true
            }

            R.id.action_settings -> {
                Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show()
                true

            }

            R.id.action_search -> {
                //展开/收起监听
                item.setOnActionExpandListener(object : OnActionExpandListener {
                    override fun onMenuItemActionExpand(item: MenuItem): Boolean {
                        Toast.makeText(this@ActionBarActivity, "Expand", Toast.LENGTH_SHORT).show()
                        return true
                    }

                    override fun onMenuItemActionCollapse(item: MenuItem): Boolean {
                        Toast.makeText(this@ActionBarActivity, "Collapse", Toast.LENGTH_SHORT)
                            .show()
                        return true
                    }
                })
                val searchView = item.actionView as SearchView
                //搜索按钮监听
                searchView.setOnQueryTextListener(object : OnQueryTextListener {
                    //点击搜索按钮时回调
                    override fun onQueryTextSubmit(query: String?): Boolean {
                        Toast.makeText(this@ActionBarActivity, query, Toast.LENGTH_SHORT).show()
                        return true
                    }

                    override fun onQueryTextChange(newText: String?): Boolean {
                        Log.d("onQueryTextChange", "newText: $newText")
                        return true
                    }
                })
                searchView.queryHint = "请输入电话号码..."

                true
            }

            R.id.action_share -> {
                Toast.makeText(this,"share",Toast.LENGTH_SHORT).show()
                val shareActionProvider =
                    MenuItemCompat.getActionProvider(item) as ShareActionProvider
                shareActionProvider.setOnShareTargetSelectedListener { source, intent ->
                    Toast.makeText(this,"$source",Toast.LENGTH_SHORT).show()
                    true
                }
                Intent(Intent.ACTION_SEND).apply {
                    setType("image/*")
                    putExtra(Intent.EXTRA_STREAM,"")
                }.also {
                    shareActionProvider.setShareIntent(it)
                }
                true
            }

            else -> super.onOptionsItemSelected(item)
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {

        return super.onKeyDown(keyCode, event)
    }

    companion object {
        fun start(context: Context) {
            Intent(context, ActionBarActivity::class.java).also {
                context.startActivity(it)
            }
        }
    }
}